package com.lxw.weather.adapter;

import android.content.Context;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.lxw.weather.R;
import com.lxw.weather.model.HourlyForecast;
import java.util.List;

public class HourlyForecastAdapter extends RecyclerView.Adapter<HourlyForecastAdapter.HourlyForecastViewHolder> {

    private final List<HourlyForecast> hourlyForecastList;
    private final Context context;
    private boolean isMultiItemMode = false; // 控制是否显示多个item

    public HourlyForecastAdapter(List<HourlyForecast> hourlyForecastList, Context context) {
        this.hourlyForecastList = hourlyForecastList;
        this.context = context;
    }

    // 设置是否显示多个item（可选）
    public void setMultiItemMode(boolean isMultiItemMode) {
        this.isMultiItemMode = isMultiItemMode;
    }

    // 添加辅助方法：dp转px
    private int dpToPx(int dp, Context context) {
        DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
        return Math.round(dp * (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT));
    }

    @Override
    public HourlyForecastViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.hourly_forecast_item, parent, false);
    
        // 设置100dp宽度
        int widthInDp = 85;
        int widthInPx = dpToPx(widthInDp, parent.getContext());
    
        ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
        layoutParams.width = widthInPx;
        view.setLayoutParams(layoutParams);
    
        return new HourlyForecastViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull HourlyForecastViewHolder holder, int position) {
        HourlyForecast forecast = hourlyForecastList.get(position);

        // 绑定数据
        holder.hourText.setText(formatHour(forecast.getHour()));
        holder.hourlyTemp.setText(formatTemperature(forecast.getTemp()));
        holder.hourlyWeatherCondition.setText(forecast.getWeatherCondition());

        // 设置天气图标
        holder.hourlyWeatherIcon.setImageResource(getWeatherIconRes(forecast.getWeatherIcon()));

        // 绑定风力信息
        String windInfo = formatWindInfo(forecast.getWindDirection(), forecast.getWindPower());
        holder.hourlyWind.setText(windInfo);

        // 绑定温度范围
        String tempRange = formatTempRange(forecast.getMinTemp(), forecast.getMaxTemp());
        holder.hourlyMinMaxTemp.setText(tempRange);
    }

    @Override
    public int getItemCount() {
        return hourlyForecastList == null ? 0 : hourlyForecastList.size();
    }

    // 辅助方法：格式化小时显示
    private String formatHour(String hour) {
        // 示例：将"08:00"转为"8时"
        return hour.replace(":00", "时").replace("0", "");
    }

    // 辅助方法：格式化温度显示
    private String formatTemperature(String temp) {
        return temp + "°";
    }

    // 辅助方法：格式化风力信息
    private String formatWindInfo(String direction, String power) {
        if (direction == null || power == null) {
            return "风力：未知";
        }
        return direction + " " + power;
    }

    // 辅助方法：格式化温度范围
    private String formatTempRange(String min, String max) {
        if (min == null || max == null) {
            return "温区：--";
        }
        return min + "°/" + max + "°";
    }

    // 获取天气图标资源
    private int getWeatherIconRes(String iconCode) {
        if (iconCode == null) return R.mipmap.d00;

        try {
            // 处理可能的扩展名（如"d00.png" -> "d00"）
            String resourceName = iconCode.contains(".") ?
                    iconCode.substring(0, iconCode.indexOf('.')) : iconCode;
            return R.mipmap.class.getField(resourceName).getInt(null);
        } catch (Exception e) {
            return R.mipmap.d00; // 默认图标
        }
    }

    public static class HourlyForecastViewHolder extends RecyclerView.ViewHolder {
        TextView hourText, hourlyTemp, hourlyWeatherCondition, hourlyWind, hourlyMinMaxTemp;
        ImageView hourlyWeatherIcon;

        public HourlyForecastViewHolder(@NonNull View itemView) {
            super(itemView);
            hourText = itemView.findViewById(R.id.hourText);
            hourlyTemp = itemView.findViewById(R.id.hourlyTemp);
            hourlyWeatherCondition = itemView.findViewById(R.id.hourlyWeatherCondition);
            hourlyWeatherIcon = itemView.findViewById(R.id.hourlyWeatherIcon);
            hourlyWind = itemView.findViewById(R.id.hourlyWind);
            hourlyMinMaxTemp = itemView.findViewById(R.id.hourlyMinMaxTemp);
        }
    }
}